from operate import left_click, pic_click, confirm_step, press
from util import go_position, go_back, go_home
import time
from variable import Variable


def finish_dorm_touch(number, x, y):
    print("进入{}号宿舍".format(number))
    go_position("dorm")
    if not left_click(x, y):
        return
    for index, i in enumerate([2.3, 1.75, 1.45]):
        time.sleep(2)
        print("抚摸第{}个".format(index + 1))
        left_click(Variable.width / 1.108, Variable.height / i, delay=1)
        if not confirm_step("./images/dorm/touch.png", w=230, h=350, bl=1310, bt=390):
            print("点击头像失败")
            continue
        time.sleep(3)
        left_click(Variable.width / 1.108, Variable.height / i, delay=0.5)
        if not confirm_step("./images/dorm/touch.png", w=230, h=350, bl=1310, bt=390):
            print("点击头像失败")
            continue
        left_click(Variable.width / 1.216, Variable.height / i)
        for j in range(3):
            if not confirm_step(
                "./images/dorm/touch_one.png", w=130, h=130, bl=765, bt=840
            ):
                print("页面位置不对，开始下一个")
                break
            xy = confirm_step(
                "./images/dorm/no_touch.png", confidence=0.6, w=50, h=70, bl=1550, bt=90
            )
            if xy:
                break
            press("z")
            time.sleep(5)
        go_back()
    time.sleep(1)
    go_back()
    time.sleep(1)


def finish_dorm_zhiqin():
    print("查看宿舍执勤")
    go_position("dorm")
    if not pic_click(src="./images/dorm/zhiqin.png"):
        print("找不到执勤图标")
        return False
    xy = confirm_step("./images/dorm/rest.png", 0.8, 480, 180, 720, 520)
    if xy:
        print("休息中...")
        go_back()
        return
    left_click(Variable.width / 1.8, Variable.height / 1.6)
    for i in range(8):
        if not pic_click("./images/dorm/tili100.png", delay=0.5):
            press("up")
            time.sleep(0.2)
            pic_click("./images/dorm/tili100.png", delay=0.5)
    pic_click("./images/dorm/start_zhiqin.png", 1)
    # 开始执勤
    if Variable.dorm["一键代工"]:
        # 一键代工
        left_click(
            Variable.width - 180 / 2 * Variable.scale,
            Variable.height - 30 / 2 * Variable.scale,
        )
        left_click(
            Variable.width - 277 / 2 * Variable.scale,
            Variable.height - 81 / 2 * Variable.scale,
        )
    go_back()
    go_back()
    time.sleep(1)


def finish_dorm_task():
    print("查看宿舍任务列表")
    go_position("dorm")
    if not pic_click(src="./images/dorm/task.png"):
        print("找不到宿舍任务图标")
        return False
    left_click(Variable.width / 10, Variable.height / 1.7)
    # while pic_click(
    #     src="./images/dorm/receive.png",
    #     delay=1,
    #     confidence=0.9,
    # ):
    #     go_back()
    if not pic_click(src="./images/tasks/all_receive.png", confidence=0.6):
        print("找不到领取按钮")
    go_back()
    print("奖励领取完毕")


def finish_produce():
    print("开始制造三次家具")
    go_position("dorm")
    if not pic_click("./images/dorm/produce.png"):
        print("找不到制造按钮，结束制造")
        return
    left_click(Variable.width / 1.6, Variable.height / 2)
    if confirm_step("./images/dorm/produce_choose.png"):
        # 添加类型
        left_click(Variable.width / 1.47, Variable.height / 1.96)

        for i in Variable.dorm["produce_type"]:
            if not pic_click("./images/dorm/" + i + ".png"):
                press("up")
                pic_click("./images/dorm/" + i + ".png")
        left_click(Variable.width / 1.43, Variable.height / 1.12)
        # 投入家具币
        for i in [[4, 2.6], [3, 1.93], [3, 1.53]]:
            for j in range(i[0]):
                left_click(Variable.width / 2.3, Variable.height / i[1], delay=0.3)
        time.sleep(0.5)
        for i in range(2):
            left_click(Variable.width / 2.7, Variable.height / 1.135, delay=0.3)
        pic_click("./images/dorm/produce_ok.png")
        time.sleep(2)
        # 领取
        left_click(Variable.width / 1.17, Variable.height / 2.38)
        left_click(Variable.width / 1.118, Variable.height / 6.11, delay=0.5)
        go_back()


def finish_dorm():
    print("开始宿舍任务")
    go_position("dorm")
    # 开始交流
    rooms = [[3, 1.7], [3.6, 2.2], [1.8, 1.9], [2.1, 2.6], [1.4, 2.3], [1.6, 3.2]]
    jiDi = [2.34, 1.87, 1.54]
    total = 0
    for index, number in enumerate(Variable.dorm["room"]):
        left_click(80 / 2 * Variable.scale, Variable.height / jiDi[index])
        for room in range(0, number):
            i = rooms[room]
            total += 1
            finish_dorm_touch(total, Variable.width / i[0], Variable.height / i[1])
    print("宿舍抚摸完成")
    if Variable.dorm["执勤"]:
        finish_dorm_zhiqin()
        print("执勤完成")
    if Variable.dorm["produce"]:
        finish_produce()
        print("制造三次家具完成")
    if Variable.dorm["receive_reward"]:
        finish_dorm_task()
        print("奖励领取完成")
    print("宿舍任务完成")
    go_home()


if __name__ == "__main__":
    finish_produce()
